package cn.south.campus.business.service.impl;

import cn.south.campus.business.mapper.WalletLogMapper;
import cn.south.campus.business.model.bo.LoginContext;
import cn.south.campus.business.model.bo.LoginInfo;
import cn.south.campus.business.model.entity.WalletLog;
import cn.south.campus.business.model.enums.WalletEnums;
import cn.south.campus.business.model.req.WalletLogReq;
import cn.south.campus.business.model.vo.WalletLogVo;
import cn.south.campus.business.service.WalletLogService;
import cn.south.campus.common.utils.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * <p>
 *
 * </p>
 *
 * @author chow
 * @since 2024/11/14 13:37
 */
@Service
@AllArgsConstructor
public class WalletLogServiceImpl extends ServiceImpl<WalletLogMapper, WalletLog> implements WalletLogService {

    private final WalletLogMapper walletLogMapper;

    @Override
    public void insertLog(BigDecimal amount, Long userId, String orderNo, WalletEnums.WalletTypeEnums enums) {
        WalletLog walletLog = new WalletLog();
        walletLog.setUserId(userId);
        walletLog.setAmount(amount);
        walletLog.setOrderNo(orderNo);
        walletLog.setType(enums.getType());
        walletLog.setDescription(enums.getDesc());
        walletLogMapper.insert(walletLog);
    }

    @Override
    public void insertLog(BigDecimal amount, Long userId, String orderNo, WalletEnums.WalletTypeEnums enums, WalletEnums.WalletLogStatusEnums statusEnums) {
        WalletLog walletLog = new WalletLog();
        walletLog.setUserId(userId);
        walletLog.setAmount(amount);
        walletLog.setOrderNo(orderNo);
        walletLog.setType(enums.getType());
        walletLog.setStatus(statusEnums.getCode());
        walletLog.setDescription(enums.getDesc());
        walletLogMapper.insert(walletLog);
    }


    @Override
    public IPage<WalletLogVo> queryRecord(WalletLogReq req) {
        LoginInfo loginInfo = LoginContext.getContext();
        LambdaQueryWrapper<WalletLog> wrapper = new LambdaQueryWrapper<WalletLog>()
                .eq(WalletLog::getUserId, loginInfo.getId())
                .eq(req.getType() != null, WalletLog::getType, req.getType())
                .in(!req.isFreeze(), WalletLog::getStatus, WalletEnums.WalletLogStatusEnums.RECEIVED.getCode(), WalletEnums.WalletLogStatusEnums.PAYED.getCode(), WalletEnums.WalletLogStatusEnums.WITHDRAWED.getCode())
                .in(req.isFreeze(), WalletLog::getStatus, WalletEnums.WalletLogStatusEnums.PENDING.getCode(), WalletEnums.WalletLogStatusEnums.RECEIVING.getCode())
                .between(req.getStartTimeStr() != null && req.getEndTimeStr() != null, WalletLog::getCreateTime, DateUtil.parseString(req.getStartTimeStr()), DateUtil.parseString(req.getEndTimeStr()))
                .orderByDesc(WalletLog::getCreateTime);
        Page<WalletLog> walletLogPage = walletLogMapper.selectPage(req.getPageDto(), wrapper);
        return walletLogPage.convert(WalletLogVo::convert);
    }

    @Override
    public void deleteLog(Long userId, String bizNo) {
        LambdaQueryWrapper<WalletLog> wrapper = new LambdaQueryWrapper<WalletLog>()
                .eq(WalletLog::getOrderNo, bizNo).eq(WalletLog::getUserId, userId);
        walletLogMapper.delete(wrapper);
    }

    @Override
    public void updateLog(Long userId, String bizNo, WalletEnums.WalletLogStatusEnums statusEnums) {
        LambdaUpdateWrapper<WalletLog> wrapper = new LambdaUpdateWrapper<WalletLog>()
                .set(WalletLog::getStatus, statusEnums.getCode())
                .eq(WalletLog::getUserId, userId)
                .eq(WalletLog::getOrderNo, bizNo);
        walletLogMapper.update(null, wrapper);
    }
}
